package com.xie.controller;

import com.xie.pojo.User;
import com.xie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    //controller  调 service 层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    //跳转到登陆页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    //登录
    @RequestMapping("/loginUser")
    public String loginUser(User user, Model model){
        User user1 = userService.loginUser(user);
        System.out.println(user1);
        if(user1 != null){
            model.addAttribute("user",user1);
            if(user1.getAdmi().equals("1")){
                return "redirect:/dog/allDog";
            }else{
                return "redirect:/dog/allDog2";
            }
        }else{
            model.addAttribute("msg","登录失败");
            return "login";
        }
    }

    //跳转注册页面
    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "regiest";
    }
    //注册用户
    @RequestMapping("/addUser")
    public String addUser(User user,Model model){
        System.out.println("addUser=>" + user);
        int i = userService.addUser(user);
        if(i > 0){
            model.addAttribute("msg","注册成功");
            return "login";
        }else{
            model.addAttribute("msg","注册失败");
            return "login";
        }
    }



}
